package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.EmailBoot;
import com.project.bean.EmailBean;
import com.project.bean.EmployeeBean;
import com.project.service.IEmailService;
import com.project.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("email")
public class EmailController {
    @Autowired
    private IEmailService service;

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("send")
    public String send(EmailBean email,
                       HttpServletRequest req) {
        //获取session对象
        HttpSession session = req.getSession();
        //获取登陆用户
        EmployeeBean loginUser = (EmployeeBean) session.getAttribute("loginUser");

        Integer[] emIdArray = (Integer[]) session.getAttribute("emIdArray");

        String receivers = "";

        List<EmployeeBean> list = employeeService.findByIdArray(emIdArray);

        for (EmployeeBean em : list) {
            receivers += em.getRealName() + ",";
        }
        receivers = receivers.substring(0, receivers.length() - 1);

        email.setSenderId(loginUser.getEmId());
        email.setReceivers(receivers);
        service.send(email, emIdArray);

        return "ok";
    }

    @RequestMapping("findByItem")
    public IPage<EmailBean> findByItem(Integer pageNO,
                                       String head,
                                       LocalDate startDate,
                                       LocalDate endDate,
                                       HttpServletRequest req) {

        EmployeeBean employee = (EmployeeBean) req.getSession().getAttribute("loginUser");

        return service.cutByItem(pageNO, employee.getEmId(), head, startDate, endDate);
    }

    @RequestMapping("findById")
    public EmailBean findById(Integer emailId) {
        return service.findById(emailId);
    }

    @RequestMapping("del")
    public String del(Integer emailId) {
        service.del(emailId);
        return "ok";
    }

    @RequestMapping("upload")
    public String upload(@RequestParam("annex") MultipartFile mf) throws Exception {
        //得到上传文件的名称
        String filename = mf.getOriginalFilename();
        //重命名文件，防止文件重名
        filename = System.currentTimeMillis() + filename;
        //将上传文件的二进制数据保存到指定文件
        mf.transferTo(new File("C:/Users/76618/Desktop/myRepository/project/emailPrpject/src/main/resources/static/annex/" + filename));
        return filename;
    }
}
